package JDIC;

import Utils.MSJ_JOptionPane;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * <b>Use this class to browse - open - print - edit files and mail to any
 * e-mail address.</b>
 *
 * @author Mohamed Tarek Salama
 * @version 1.0 BETA
 */
public class MSJ_Desktop {

    private static Desktop desk = Desktop.getDesktop();

    /**
     * Open a web site in your default web browser<br>
     *
     * @param siteaddress :String
     */
    public static void browse(String siteaddress) {
        if (Desktop.isDesktopSupported()) {
            try {
                desk.browse(new URI(siteaddress));
            } catch (URISyntaxException ex) {
            } catch (IOException ex) {
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("Error in Desktop support");
        }
    }

    /**
     * Open path in file explorer <br> <i>! Remember to pass the file directory
     * correctly</i>
     *
     * @param filepath :String
     */
    public static void open(String filepath) {
        if (Desktop.isDesktopSupported()) {
            try {
                desk.open(new File(filepath));
            } catch (IOException ex) {
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("Error in Desktop support");
        }
    }

    /**
     * Print without show the printing window<br> <i>! Remember to pass the file
     * directory correctly</i>
     *
     * @param filepath : String
     */
    public static void print(String filepath) {
        if (Desktop.isDesktopSupported()) {
            try {
                desk.print(new File(filepath));
            } catch (IOException ex) {
            }
        }
    }

    /**
     * Edit text files by passing its path<br> <i>! Remember to pass the file
     * directory correctly</i>
     *
     * @param filepath : String
     */
    public static void edit(String filepath) {
        if (Desktop.isDesktopSupported()) {
            try {
                desk.open(new File(filepath));
            } catch (IOException ex) {
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("Error in Desktop support");
        }
    }

    /**
     * send email to the <b>toemail</b> using the default mail application<br>
     * <i>! Remember to pass the file directory correctly</i>
     *
     * @param email : String
     */
    public static void mailto(String toemail) {
        if (Desktop.isDesktopSupported()) {
            try {
                desk.mail(new URI("mailto:" + toemail));
            } catch (URISyntaxException ex) {
            } catch (IOException ex) {
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("Error in Desktop support");
        }
    }
}